package com.gqb.lucene.PageQuery;

import java.io.IOException;
import java.nio.file.Paths;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.management.Query;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.xml.builders.RangeQueryBuilder;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.TopFieldCollector;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.SortField.Type;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.poi.hwpf.usermodel.Range;


public class RangeQueryTry {
	public static Directory indexDir;
	public static IndexReader reader;
	public static IndexSearcher is;
	public static void main(String[] args) throws ParseException {
//		index();
		String fileName="dadsa.doc";
		String type=fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
		System.out.println(type);
		try {
			/*SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy");
			SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd");
			Date date1=simpleDateFormat.parse("2008");
			Date date2=simpleDateFormat1.parse("2008-01-01");
			System.out.println(date1.getTime());
			System.out.println(date2.getTime());*/
//			search();
			searchBoolean();
//			tpfSearch();
//			test();
		} /*catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} */catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void search() throws IOException, ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		
		Date startDate=sdf.parse("2011-09-16");
		Date endDate=sdf.parse("2014-04-06");
		indexDir=FSDirectory.open(Paths.get("d://luceneOA"));
		reader=DirectoryReader.open(indexDir);
		is=new IndexSearcher(reader);
		NumericRangeQuery<Long> query=NumericRangeQuery.newLongRange("date", startDate.getTime(), endDate.getTime(), true, true);
		TopDocs hits= is.search(query, 10);
		for (ScoreDoc scoreDoc: hits.scoreDocs) {
			Document document=is.doc(scoreDoc.doc);
			Date dates=new Date(Long.parseLong(document.get("date")));
			System.out.println(document.get("type")+"——"+dates.toLocaleString());
		}
	}
	
	public static void searchBoolean() throws IOException, ParseException{
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Date startDate=sdf.parse("2007-09-16");
		Date endDate=sdf.parse("2014-04-06");
		indexDir=FSDirectory.open(Paths.get("d://lucene"));
		reader=DirectoryReader.open(indexDir);
		is=new IndexSearcher(reader);
		BooleanQuery booleanQuery=new BooleanQuery();
		NumericRangeQuery<Long> query=NumericRangeQuery.newLongRange("date", startDate.getTime(), endDate.getTime(), true, true);
		Term term=new Term("indexFrom", "1");
		TermQuery termQuery=new TermQuery(term);
		booleanQuery.add(query, Occur.MUST);
		booleanQuery.add(termQuery,Occur.MUST);
		TopDocs hits= is.search(booleanQuery, 10);
		for (ScoreDoc scoreDoc: hits.scoreDocs) {
			Document document=is.doc(scoreDoc.doc);
			Date dates=new Date(Long.parseLong(document.get("date")));
			System.out.println(document.get("indexFrom")+"——"+dates.toLocaleString());
		}
	
	}
	
	/**
	 * 通配符查询
	 * @throws IOException 
	 */
	public static void tpfSearch() throws IOException{
		indexDir=FSDirectory.open(Paths.get("d://Lucene"));
		reader=DirectoryReader.open(indexDir);
		is=new IndexSearcher(reader);
		BooleanQuery booleanQueryF=new BooleanQuery();
		BooleanQuery booleanQuery=new BooleanQuery();
		Term term=new Term("id", "127484");
		TermQuery idQuery=new TermQuery(term);
		Term term2=new Term("lwdw", "*国土*");
		WildcardQuery query=new WildcardQuery(term2);
		booleanQuery.add(idQuery, Occur.MUST);
		booleanQuery.add(query,Occur.MUST);
		booleanQueryF.add(booleanQuery, Occur.MUST);
		TopDocs hits= is.search(booleanQueryF, 10);
		for (ScoreDoc scoreDoc: hits.scoreDocs) {
			Document document=is.doc(scoreDoc.doc);
			System.out.println(document.get("id"));
		}
		
	}
	public static void test() throws IOException{
		indexDir=FSDirectory.open(Paths.get("d://Lucene"));
		reader=DirectoryReader.open(indexDir);
		is=new IndexSearcher(reader);
		BooleanQuery booleanQuery = new BooleanQuery();
		Term term = new Term("indexFrom", "1");
		TermQuery termQuery = new TermQuery(term);
		booleanQuery.add(termQuery, Occur.MUST);
		Term term1=new Term("lwdw","宁波市国土资源局");
		TermQuery lwdwQuery=new TermQuery(term1);
		booleanQuery.add(lwdwQuery, Occur.MUST);
		Sort sort=new Sort(new SortField("date",Type.LONG,true));
		TopFieldCollector topFieldCollector = TopFieldCollector.create(sort,1 * 13, false,
				false, false);
		is.search(booleanQuery, topFieldCollector);
		TopDocs hits = topFieldCollector.topDocs(
				0 * 13,
				1 *13);
		for (ScoreDoc scoreDoc : hits.scoreDocs) {
			System.out.println(scoreDoc.score);
		}
		
	}
	public static List<Date> getDates() throws ParseException{
		List<Date> dates=new ArrayList<Date>();
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		dates.add(sdf.parse("2008-06-16"));
		
		dates.add(sdf.parse("2008-10-10"));
		dates.add(sdf.parse("2008-11-11"));
		dates.add(sdf.parse("2015-04-29"));
		dates.add(sdf.parse("2014-04-06"));
		dates.add(sdf.parse("2013-09-16"));
		dates.add(sdf.parse("2013-04-16"));
		dates.add(sdf.parse("2012-09-16"));
		dates.add(sdf.parse("2011-09-16"));
		dates.add(sdf.parse("2010-09-16"));
		
		return dates;
		
	}
	
	public  static void index(){
		try {
			List<Date> dates=getDates();
			Indexer indexer=Indexer.getInstance("d://luceneOA");
			/*for (Date date : dates) {
				Document document=new Document();
				document.add(new StringField("type","a",Field.Store.YES));
				document.add(new LongField("date", date.getTime(),Field.Store.YES));
				indexer.index(document);
			}
			for (Date date : dates) {
				Document document=new Document();
				document.add(new StringField("type","b",Field.Store.YES));
				document.add(new LongField("date", date.getTime(),Field.Store.YES));
				indexer.index(document);
			}*/
			Document document=new Document();
			document.add(new StringField("humanIds", "[10],[121],[161],[678]", Field.Store.YES));
			indexer.index(document);
			Document document1=new Document();
			document1.add(new StringField("humanIds", "[10],[21]", Field.Store.YES));
			indexer.index(document1);
			Document document2=new Document();
			document2.add(new StringField("humanIds", "[10],[22],[678]", Field.Store.YES));
			indexer.index(document2);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
